<template>
  <div>
    <FormPanel ref="from" :list="formList" @formChange="changeHandle" :cols="2" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <div style="height:47px;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer(1)">关 闭</el-button>
      <multiuse-button type="primary" :click="closeDrawer.bind(this, 2)">确 认</multiuse-button>
    </div>
  </div>
</template>
<script>
import {detailFundAdjust,confirmFundAdjust,getTtFiAccountList} from '@/api/omSale/OrderManagement/OrderAllocation/index.js'
export default {
  data(){
    this.saveData=null;
    return{
      formList:this.createdFrom(),
      aclist:[]
    }
  },
  props:['list'],
  created(){
    this.gettmFiAccountType(this.list)
    this.getDetail(this.list)
  },
  methods:{
    createdFrom(){
      return [
        {
          type: 'INPUT',
          label: '所选订单总金额',
          fieldName: 'allAfterDiscountRebateAmount',
          placeholder: '请选择',
          disabled:true,
        },
        {
          type: 'INPUT',
          label: '账户总金额',
          fieldName: 'allAvailableAmount',
          placeholder: '请输入',
          disabled:true,
        },
        {
          type: 'SELECT',
          label: '资金类型',
          fieldName: 'accountType',
          placeholder: '请选择',
          itemList:[],
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
          change:p=>{
            if(p){
              this.changeAccountType(p)
            }else{
              this.formList.find(x=>x.fieldName=='zhkyye').initialValue=''
            }
          }
        },
        {
          type: 'INPUT',
          label: '账户可用余额',
          fieldName: 'zhkyye',
          placeholder: '请输入',
          disabled:true,
        },


      ]
    },
    async getDetail(list){
      let arr=[];
      list.map(x=>{
        arr.push(x.id);
      })
      let res=await detailFundAdjust({ids:arr})
      console.log(res,'www')
      for(let i in res.data){
        this.formList.map(x=>{
          if(x.fieldName==i){
            x.initialValue=res.data[i]
          }
        })
      }
    },
    // 加载资金类型
    async gettmFiAccountType(list){
      let json={ownerCode:list[0].dealerCode,powerType:list[0].powerCompany,fundsType:0}
      let res=(await getTtFiAccountList(json)).data
      this.aclist=res;
      this.formList.find(x=>x.fieldName=='accountType').itemList=res.map(x=>({text:`${x.accountName}--${x.availableAmount}元`,value:x.id}))
    },
    changeAccountType(id){
      let str='';
      let arr=this.aclist;
      arr.map(x=>{
        if(id==x.id){
          str=x.availableAmount
        }
      })
      this.formList.find(x=>x.fieldName=='zhkyye').initialValue=str;
    },
    changeHandle(val){
      this.saveData=val;
    },
    async closeDrawer(type){
      if(type==1){
        this.$emit('close')
      }else{
        this.$refs.from.SUBMIT_FORM();
        if (this.saveData ) {
          let arr=[]
          this.list.map(x=>{
            arr.push(x.id)
          })
          let json=JSON.parse(JSON.stringify(this.saveData))
          json.id=json.accountType;
          json.ids=arr;
          let res=await confirmFundAdjust(json)
            if (res.resultCode == 200) {
              this.$notify({
                title: '成功',
                message: '调整成功',
                type: 'success'
              });
              this.$emit('close', json);
            }else{
              this.saveData=null;
            }
        }
      }
    }
  }
}
</script>
